CC = cc
CFLAGS = --std=c99 -g3 
ALL_SRC = $(wildcard *.c)
SRC = $(wildcard cds_*.c)
OBJS = $(patsubst %.c,%.o,$(SRC))


AVL_SRC = $(SRC)
AVL_SRC += test_avl.c
AVL_OBJS = $(OBJS)
AVL_OBJS += test_avl.o

LIST_SRC = $(SRC)
LIST_SRC += test_list.c
LIST_OBJS = $(OBJS)
LIST_OBJS += test_list.o

RBTREE_SRC = $(SRC)
RBTREE_SRC += test_rbtree.c
RBTREE_OBJS = $(OBJS)
RBTREE_OBJS += test_rbtree.o

BTREE_SRC = $(SRC)
BTREE_SRC += test_btree.c
BTREE_OBJS = $(OBJS)
BTREE_OBJS += test_btree.o


.PHONY: clean

cds.a: $(OBJS) 
	ar rcs libcds.a cds_avl.o

test_avl: CFLAGS += -D DEBUG 
test_avl: $(AVL_OBJS)
	$(CC) -o $@ $^

test_rbtree: CFLAGS += -D DEBUG
test_rbtree: $(RBTREE_OBJS)
	$(CC) -o $@ $^

test_btree: CFLAGS += -D DEBUG
test_btree: $(BTREE_OBJS)
	$(CC) -o $@ $^

test_list: CFLAGS += -D DEBUG 
test_list: $(LIST_OBJS)
	$(CC) -o $@ $^
	
%.d: %.c
	@set -e;rm -f $@;\
	$(CC) -MM $< >$@.$$$$;\
	sed 's/\($*\)\.o[ :]*/\1.o $@ :/g'<$@.$$$$>$@;\
	rm -f $@.$$$$

include $(ALL_SRC:.c=.d)

print:
	@echo "SRC:$(SRC)"
	@echo "OBJS:$(OBJS)"
	@echo "AVL_OBJS: $(AVL_OBJS)"
	@echo "LIST_OBJS: $(LIST_OBJS)" 

clean:
	rm -rf test_avl test_list test_rbtree test_btree *.o *.d *.a *.so *.h.gch

